package com.joven.core.controller;

import com.joven.core.utils.BizException;
import com.joven.core.constant.ResultInfo;
import com.joven.core.constant.Status;
import com.joven.core.model.User;
import com.joven.core.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author joven_wong
 * 2021/5/26 15:07
 */
@RestController
@RequestMapping("/user")
public class UserController {

    private UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @GetMapping("/findAllUser")
    public List<User> findAllUser(){
        return userService.findAllUser();
    }

    @GetMapping("/resultUsers")
    public ResultInfo<List<User>> resultUsers(){
        List<User> users = userService.findAllUser();
        return ResultInfo.success(users);
    }

    @GetMapping("/getOne")
    public String getUser(Long id){
        return userService.getUsernameById(id);
    }

    @GetMapping("/exception")
    public ResultInfo testException(){
        throw new BizException(Status.SYSTEM_ERROR);
    }

    @GetMapping("/exception1")
    public ResultInfo testException1(String str){
        if(str.equals("test")){
            return ResultInfo.success("Test success");
        }
        return ResultInfo.fail("Test fail");
    }
}
